package org.szy.plugin.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.mybatisflex.core.paginate.Page;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.plugin.sys.entity.SysApp;
import org.szy.plugin.sys.param.SysAppQueryParam;
import org.szy.plugin.sys.service.SysAppService;

import java.util.List;


/**
 * 系统应用控制器
 *
 * @author wangbuhao
 * @since 2023-06-21 17:04:20
 */
@Controller
@Mapping("/sys/app")
public class SysAppController extends BaseController<SysApp> {

    private final String PATH_PREFIX = "sys/app/";
    @Inject
    private SysAppService sysAppService;

    /**
     * 系统应用列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 系统应用编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        return view;
    }

    /**
     * 获取系统应用分页数据
     *
     * @author wangbuhao
     * @since 2023-06-21 17:04:20
     */
    @Get
    @Mapping("/page")
    @SaCheckPermission(value = "sys_app_list", mode = SaMode.OR, orRole = "wangbuhao")
    public R<Page<SysApp>> page(SysAppQueryParam param) {
        return R.data(sysAppService.page(getPage(), param));
    }

    @Get
    @Mapping("/list")
    public R<List<SysApp>> list(SysAppQueryParam param) {
        return R.data(sysAppService.list(param));
    }

    /**
     * 获取系统应用详情
     *
     * @author wangbuhao
     * @since 2023-06-21 17:04:20
     */
    @Get
    @Mapping("/getInfo")
    @SaCheckPermission(value = "sys_app_detail", mode = SaMode.OR, orRole = "wangbuhao")
    public R<SysApp> getInfo(@NotNull Long id) {
        return R.asNull(sysAppService.getById(id));
    }

    /**
     * 添加系统应用
     *
     * @author wangbuhao
     * @since 2023-06-21 17:04:20
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_app_add", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> add(@Validated SysApp entity) {
        boolean save = sysAppService.add(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑系统应用
     *
     * @author wangbuhao
     * @since 2023-06-21 17:04:20
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_app_update", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> update(@Validated SysApp entity) {
        boolean update = sysAppService.updateById(entity);
        return R.bool(update);
    }

    /**
     * 删除系统应用
     *
     * @author wangbuhao
     * @since 2023-06-21 17:04:20
     */
    @Delete
    @Mapping("/delete")
    @SaCheckPermission(value = "sys_app_delete", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> delete(@NotNull Long id) {
        boolean delete = sysAppService.removeById(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 修改系统应用状态
     *
     * @param param 公共状态参数
     * @return 修改结果
     */
    @Put
    @Mapping("/modifyStatus")
    public R<?> modifyStatus(CommonStatusParam param) {
        boolean status = sysAppService.modifyStatus(param);
        return R.bool(status);
    }

    /**
     * 修改系统应用选择
     *
     * @param param 公共状态参数
     * @return 修改结果
     */
    @Put
    @Mapping("/modifyActive")
    public R<?> modifyActive(CommonStatusParam param) {
        boolean status = sysAppService.modifyActive(param);
        return R.bool(status);
    }

}
